// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.input;

import java.util.ArrayList;
import org.lwjglx.input.Controller;
import org.lwjglx.input.Keyboard;
import org.lwjglx.input.Mouse;
import zombie.Lua.LuaEventManager;
import zombie.Lua.LuaManager;
import zombie.core.Core;
import zombie.input.ControllerState;
import zombie.input.ControllerStateCache;
import zombie.input.GameKeyboard;
import zombie.input.JoypadManager;

public final class Input {
    public static final int ANY_CONTROLLER = -1;
    private final Controller[] controllers = new Controller[16];
    private final ArrayList<Controller> newlyConnected = new ArrayList();
    private final ArrayList<Controller> newlyDisconnected = new ArrayList();
    private final boolean[][] controllerPressed = new boolean[16][15];
    private final boolean[][] controllerWasPressed = new boolean[16][15];
    private final float[][] controllerPov = new float[16][2];
    private final ControllerStateCache m_controllerStateCache = new ControllerStateCache();

    public static String getKeyName(int code) {
        String string = Keyboard.getKeyName(code);
        if ("LSHIFT".equals(string)) {
            string = "Left SHIFT";
        }

        if ("RSHIFT".equals(string)) {
            string = "Right SHIFT";
        }

        if ("LMENU".equals(string)) {
            string = "Left ALT";
        } else if ("RMENU".equals(string)) {
            string = "Right ALT";
        }

        return string;
    }

    public static int getKeyCode(String keyName) {
        if ("Right SHIFT".equals(keyName)) {
            return 54;
        } else if ("Left SHIFT".equals(keyName)) {
            return 42;
        } else if ("Left ALT".equals(keyName)) {
            return 56;
        } else {
            return "Right ALT".equals(keyName) ? 184 : Keyboard.getKeyIndex(keyName);
        }
    }

    public int getControllerCount() {
        return this.controllers.length;
    }

    public int getAxisCount(int index) {
        Controller controller = this.getController(index);
        return controller == null ? 0 : controller.getAxisCount();
    }

    public float getAxisValue(int index, int axis) {
        Controller controller = this.getController(index);
        return controller == null ? 0.0F : controller.getAxisValue(axis);
    }

    public String getAxisName(int index, int axis) {
        Controller controller = this.getController(index);
        return controller == null ? null : controller.getAxisName(axis);
    }

    public boolean isControllerLeftD(int index) {
        if (index == -1) {
            for (int _int = 0; _int < this.controllers.length; _int++) {
                if (this.isControllerLeftD(_int)) {
                    return true;
                }
            }

            return false;
        } else {
            Controller controller = this.getController(index);
            return controller == null ? false : controller.getPovX() < -0.5F;
        }
    }

    public boolean isControllerRightD(int index) {
        if (index == -1) {
            for (int _int = 0; _int < this.controllers.length; _int++) {
                if (this.isControllerRightD(_int)) {
                    return true;
                }
            }

            return false;
        } else {
            Controller controller = this.getController(index);
            return controller == null ? false : controller.getPovX() > 0.5F;
        }
    }

    public boolean isControllerUpD(int index) {
        if (index == -1) {
            for (int _int = 0; _int < this.controllers.length; _int++) {
                if (this.isControllerUpD(_int)) {
                    return true;
                }
            }

            return false;
        } else {
            Controller controller = this.getController(index);
            return controller == null ? false : controller.getPovY() < -0.5F;
        }
    }

    public boolean isControllerDownD(int index) {
        if (index == -1) {
            for (int _int = 0; _int < this.controllers.length; _int++) {
                if (this.isControllerDownD(_int)) {
                    return true;
                }
            }

            return false;
        } else {
            Controller controller = this.getController(index);
            return controller == null ? false : controller.getPovY() > 0.5F;
        }
    }

    private Controller checkControllerButton(int int0, int int1) {
        Controller controller = this.getController(int0);
        if (controller == null) {
            return null;
        } else {
            return int1 >= 0 && int1 < controller.getButtonCount() ? controller : null;
        }
    }

    public boolean isButtonPressedD(int button, int index) {
        if (index == -1) {
            for (int _int = 0; _int < this.controllers.length; _int++) {
                if (this.isButtonPressedD(button, _int)) {
                    return true;
                }
            }

            return false;
        } else {
            Controller controller = this.checkControllerButton(index, button);
            return controller == null ? false : this.controllerPressed[index][button];
        }
    }

    public boolean wasButtonPressed(int index, int button) {
        Controller controller = this.checkControllerButton(index, button);
        return controller == null ? false : this.controllerWasPressed[index][button];
    }

    public boolean isButtonStartPress(int index, int button) {
        return !this.wasButtonPressed(index, button) && this.isButtonPressedD(button, index);
    }

    public boolean isButtonReleasePress(int index, int button) {
        return this.wasButtonPressed(index, button) && !this.isButtonPressedD(button, index);
    }

    public void initControllers() {
        this.updateGameThread();
        this.updateGameThread();
    }

    private void onControllerConnected(Controller controller) {
        JoypadManager.instance.onControllerConnected(controller);
        if (LuaManager.env != null) {
            LuaEventManager.triggerEvent("OnGamepadConnect", controller.getID());
        }
    }

    private void onControllerDisconnected(Controller controller) {
        JoypadManager.instance.onControllerDisconnected(controller);
        if (LuaManager.env != null) {
            LuaEventManager.triggerEvent("OnGamepadDisconnect", controller.getID());
        }
    }

    public void poll() {
        if (!Core.getInstance().isDoingTextEntry()) {
            while (GameKeyboard.getEventQueuePolling().next()) {
            }
        }

        while (Mouse.next()) {
        }

        this.m_controllerStateCache.poll();
    }

    public Controller getController(int _int) {
        return _int >= 0 && _int < this.controllers.length ? this.controllers[_int] : null;
    }

    public int getButtonCount(int index) {
        Controller controller = this.getController(index);
        return controller == null ? null : controller.getButtonCount();
    }

    public String getButtonName(int index, int button) {
        Controller controller = this.getController(index);
        return controller == null ? null : controller.getButtonName(button);
    }

    public void updateGameThread() {
        if (!this.m_controllerStateCache.getState().isCreated()) {
            this.m_controllerStateCache.swap();
        } else {
            ControllerState controllerState = this.m_controllerStateCache.getState();
            if (this.checkConnectDisconnect(controllerState)) {
                for (int int0 = 0; int0 < this.newlyDisconnected.size(); int0++) {
                    Controller controller0 = (Controller)this.newlyDisconnected.get(int0);
                    this.onControllerDisconnected(controller0);
                }

                for (int int1 = 0; int1 < this.newlyConnected.size(); int1++) {
                    Controller controller1 = (Controller)this.newlyConnected.get(int1);
                    this.onControllerConnected(controller1);
                }
            }

            for (int int2 = 0; int2 < this.getControllerCount(); int2++) {
                Controller controller2 = this.getController(int2);
                if (controller2 != null) {
                    int int3 = controller2.getButtonCount();

                    for (int int4 = 0; int4 < int3; int4++) {
                        this.controllerWasPressed[int2][int4] = this.controllerPressed[int2][int4];
                        if (this.controllerPressed[int2][int4] && !controller2.isButtonPressed(int4)) {
                            this.controllerPressed[int2][int4] = false;
                        } else if (!this.controllerPressed[int2][int4] && controller2.isButtonPressed(int4)) {
                            this.controllerPressed[int2][int4] = true;
                            JoypadManager.instance.onPressed(int2, int4);
                        }
                    }

                    int3 = controller2.getAxisCount();

                    for (int int5 = 0; int5 < int3; int5++) {
                        float float0 = controller2.getAxisValue(int5);
                        if ((!controller2.isGamepad() || int5 != 4) && int5 != 5) {
                            if (float0 < -0.5F) {
                                JoypadManager.instance.onPressedAxisNeg(int2, int5);
                            }

                            if (float0 > 0.5F) {
                                JoypadManager.instance.onPressedAxis(int2, int5);
                            }
                        } else if (float0 > 0.0F) {
                            JoypadManager.instance.onPressedTrigger(int2, int5);
                        }
                    }

                    float float1 = controller2.getPovX();
                    float float2 = controller2.getPovY();
                    if (float1 != this.controllerPov[int2][0] || float2 != this.controllerPov[int2][1]) {
                        this.controllerPov[int2][0] = float1;
                        this.controllerPov[int2][1] = float2;
                        JoypadManager.instance.onPressedPov(int2);
                    }
                }
            }

            this.m_controllerStateCache.swap();
        }
    }

    private boolean checkConnectDisconnect(ControllerState controllerState) {
        boolean _boolean = false;
        this.newlyConnected.clear();
        this.newlyDisconnected.clear();

        for (int _int = 0; _int < 16; _int++) {
            Controller controller = controllerState.getController(_int);
            if (controller != this.controllers[_int]) {
                _boolean = true;
                if (controller != null && controller.isGamepad()) {
                    this.newlyConnected.add(controller);
                } else {
                    if (this.controllers[_int] != null) {
                        this.newlyDisconnected.add(this.controllers[_int]);
                    }

                    controller = null;
                }

                this.controllers[_int] = controller;
            }
        }

        return _boolean;
    }

    public void quit() {
        this.m_controllerStateCache.quit();
    }
}
